"""
while 循环
"""
x = 1
while x < 100:
    x += 1

name = ''
while not name:
    name = input('输入名字')


"""
for 循环
"""
words = ['this', 'is', 'an', 'ex', 'parrot']
for word in words:
    print(word)

"""
迭代字典
"""
d = {'x': 1, 'y': 2, 'z': 3}
for key in d:
    print(key, d[key], sep='--')


"""
一些迭代工具
"""
# 1. 并行迭代
names = ['anne', 'beth', 'george', 'damon']
ages = [12, 45, 32, 102]
print('----------')
print(range(len(names)))
for i in range(len(names)):
    print(names[i], 'is', ages[i], 'years', 'old', sep='_')

# 2. 迭代时获取索引
# 笨重的方法
for name in names:
    if name == 'beth':
        print(names.index(name))
# 使用内置函数 enumerate
for index, name in enumerate(names):
    if name == 'beth':
        print(index)

# 3. 反向迭代 和 排序后再迭代
arr = [4, 3, 6, 8, 3]
print(sorted(arr))
print(sorted('hello world!'))
print(reversed('hello world!'))
print(list(reversed('hello world!')))
print(''.join(reversed('hello world!')))

"""
跳出循环
"""
# 1. break
j = 1
while j < 100:
    j += 1
    if j == 5:
        break
print(j)

for item in arr:
    if item == 3:
        break

# 2. continue
for item in arr:
    if item == 3:
        continue
    print('item', item)




